<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<script type="text/javascript" src="${ctx}/js/jquery.itextclear.js"></script>
<script type="text/javascript" src="${ctx}/js/cookie.js"></script>
<script type="text/javascript">
	$(document)
			.ready(
					function() {
						$(
								'input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea',
								'.form').iTextClear();
					});
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css" />
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen"
	href="${ctx}/lib/datatables/css/vpad.css" />
<script type="text/javascript"
	src="${ctx}/lib/datatables/js/jquery.dataTables.js"></script>
<script type="text/javascript">
	/*$(document).ready(function() {
	    $('#example').dataTable({
	        "sPaginationType": "full_numbers"
	    });
	});*/
</script>
<!-- DATATABLES CSS END -->

<h1 class="page-title">职务设置</h1>

<div class="container_12 clearfix leading">

	<%--
    <%@ include file="/WEB-INF/pages/common/common_tree.jsp"%>
--%>

	<div class="tabs side grid_8 leading">

		<div class="grid_12">
			<div id="demo" class="clearfix">
				<table class="display" id="example">
					<thead>
						<tr>
							<th>选择</th>
							<th>员工工号</th>
							<th>姓名</th>
							<th>职等职级</th>
						</tr>
					</thead>
					<tbody id="employeetargetlist">
						<s:if test="#request.works==null || #request.works.size<=0">
							<tr class="gradeA">
								<td colspan="4">无数据结果</td>
							</tr>
						</s:if>
						<s:else>
							<s:iterator value="#request.works" var="d" status="sta">
								<s:if test="#sta.even">
									<tr class="gradeA">
								</s:if>
								<s:else>
									<tr class="gradeC">
								</s:else>
								<td>
									<input type="checkbox" name="checkbox_work" value="${d.id}"
									<s:iterator value="#session.duty_outing_target" var="target">
										<s:if test="#target.workId == #d.id">
											checked="checked"
										</s:if>
									</s:iterator>
									 />
								</td>
								<td>${d.workNo}</td>
								<td><span class="form-input">${d.baseId.name}</span></td>
								<td>${d.grade.gradeName}</td>
								</tr>
							</s:iterator>
						</s:else>
					</tbody>
				</table>
			</div>

			<div class="form-action clearfix">
				<p><%@ include file="/WEB-INF/pages/common/main_pager.jsp"%></p>
				<p>
					<input type="checkbox" id="form-all" value="1"
						onchange="checkAll(this)" /><label for="form-all">全选</label> <a
						class="button" href="javascript:void(0)"
						onclick="targetSelectChange()"><span
						style="background:url(${ctx}/images/img/add.png)" class="icon">&nbsp;</span>提交</a>

					<a class="button" href="javascript:void(0)"
						onclick="javascript:history.back()"><span
						style="background: url(${ctx}/images/img/arrow_undo.png)"
						class="icon">&nbsp;</span>返回</a>
				</p>
			</div>
		</div>
	</div>
</div>

<div class="clear"></div>

<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<script type="text/javascript">
	/* var TargetObject = {
			organizationId:${organizationId},dutyId : "",score:1
	} */
	var targetArray;
	$(document).ready(function(){
		if(getCookie("duty_outing_target") != null){
			targetArray = getCookie("duty_outing_target").match(/(\{(\s|\S)+?\})+?/g);
			if(targetArray == null){
				targetArray = new Array();
			}
			$("#employeetargetlist tr").each(function(i){
				var checkObj = $(this).find("input[name='checkbox_work']");
				if(targetArray.toString().indexOf("workId:" + $(checkObj).val()+",") > -1){
					for(var i=0;i<targetArray.length;i++){
						var jsonObj = eval("(" + targetArray[i] + ")");
						if(jsonObj.workId == $(checkObj).val()){
							$(this).find("input[name='checkbox_work']").attr("checked","checked");
							break;
						}
					}
				}
			});
		}else{
			targetArray = new Array();
		}
	});

	function checkAll(o) {
		if (o.checked) {
			$("input[type='checkbox'][name='checkbox_work']").attr("checked", true);
		} else {
			$("input[type='checkbox'][name='checkbox_work']").attr("checked", false);
		}
	}

	function targetSelectChange() {
		var validate = true;
		var alertMsg = "";
		$("input[name='checkbox_work']")
				.each(
						function(i) {
							if (this.checked) {
								var url = "${ctx}/ajax/train-plan-employeetarget-save.xhtml";
					            $.ajax(url, {
					                type:"POST",
					                async:false,
					                data:{"organizationId":${organizationId},
					                	"dutyId": ${dutyId},
										"workId":$(this).val()
										},
					                dataType:"html",
					                cache:false,
					                success:function (a) {
					                	//	
					                },
					                complete:function () {
					                	//	
					                }
					            });
							}else{	//如果没有选择要删掉cookie中的数据 
								var url = "${ctx}/ajax/train-plan-employeetarget-remove.xhtml";
					            $.ajax(url, {
					                type:"POST",
					                async:false,
					                data:{"organizationId":${organizationId},
					                	"dutyId": $(this).val(),
					                	"workId":$(this).val()
										},
					                dataType:"html",
					                cache:false,
					                success:function (a) {
					                	//	
					                },
					                complete:function () {
					                	//	
					                }
					            });
							}
						});
		if (!validate) {
			alert(alertMsg);
			return;
		}
		//document.cookie = "duty_outing_target=" + targetArray;
		asyncUpdateSelectBody();
	}
</script>
